package com.example.duomeitijuhe.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.duomeitijuhe.R;
import com.example.duomeitijuhe.bean.GetInternetDataBean;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael on 2014/10/15.
 */
public class InternetDataAdapter extends ArrayAdapter<GetInternetDataBean> {

    private int resourceId;
    private Context context;
    public InternetDataAdapter(Context context,int textViewResourceId,List<GetInternetDataBean> objects){
        super(context,textViewResourceId,objects);
        resourceId = textViewResourceId;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GetInternetDataBean getInternetDataBean = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.senderIconButton = (ImageButton) view.findViewById(R.id.sender_head_icon_button);
            viewHolder.senderNameText = (TextView) view.findViewById(R.id.sender_name_text);
            viewHolder.sendTimeText = (TextView) view.findViewById(R.id.send_weibo_time_text);
            viewHolder.fromPlatformText = (TextView) view.findViewById(R.id.from_platform_text);
            viewHolder.contentText = (TextView) view.findViewById(R.id.sender_weibo_content_text);
            viewHolder.contentPictureGrid = (GridView) view.findViewById(R.id.content_grid_view);
            viewHolder.originalSenderIconButton = (ImageButton) view.findViewById(R.id.original_sender_head_button);
            viewHolder.originalSenderNameText = (TextView) view.findViewById(R.id.original_sender_name_text);
            viewHolder.originalContentText = (TextView) view.findViewById(R.id.original_weibo_content_text);
            viewHolder.originalContentPicturesGrid = (GridView) view.findViewById(R.id.original_content_grid_view);
            viewHolder.retransmitButton = (Button) view.findViewById(R.id.retransmit_button);
            viewHolder.commentButton = (Button) view.findViewById(R.id.comment_button);
            viewHolder.praiseButton = (Button) view.findViewById(R.id.praise_button);

            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        int i = getInternetDataBean.getIsRetransmit();
        viewHolder.senderIconButton.setImageBitmap(getInternetDataBean.getSenderHeadBitmap());
        viewHolder.senderNameText.setText(getInternetDataBean.getSenderName());
        viewHolder.sendTimeText.setText(getInternetDataBean.getSendTime());
        viewHolder.contentText.setText(getInternetDataBean.getContent());

        ArrayList<Bitmap> bitmapArrayList = getInternetDataBean.getContentPictures();
        if (bitmapArrayList.size() != 0){
            ContentPictureAdapter contentPictureAdapter = new ContentPictureAdapter(context,bitmapArrayList);
            viewHolder.contentPictureGrid.setAdapter(contentPictureAdapter);
            viewHolder.contentPictureGrid.setVisibility(View.VISIBLE);

        }
        if (i == 1) {
            viewHolder.originalSenderIconButton.setImageBitmap(getInternetDataBean.getOriginalHeadBitmap());
            viewHolder.originalSenderNameText.setText(getInternetDataBean.getOriginalSenderName());
            viewHolder.originalContentText.setText(getInternetDataBean.getOriginalContent());

            ArrayList<Bitmap> arrayList = getInternetDataBean.getOriginalPictures();
            if (arrayList.size() != 0){
                ContentPictureAdapter contentPictureAdapter = new ContentPictureAdapter(context,arrayList);
                viewHolder.originalContentPicturesGrid.setAdapter(contentPictureAdapter);
                viewHolder.originalContentPicturesGrid.setVisibility(View.VISIBLE);
            }

        } else {
            viewHolder.originalContentText.setVisibility(View.GONE);
            viewHolder.originalSenderIconButton.setVisibility(View.GONE);
            viewHolder.originalSenderNameText.setVisibility(View.GONE);
        }
            return view;
    }

    /**
     * Control whether methods that change the list ({@link #add},
     * {@link #insert}, {@link #remove}, {@link #clear}) automatically call
     * {@link #notifyDataSetChanged}.  If set to false, caller must
     * manually call notifyDataSetChanged() to have the changes
     * reflected in the attached view.
     * <p/>
     * The default is true, and calling notifyDataSetChanged()
     * resets the flag to true.
     *
     * @param notifyOnChange if true, modifications to the list will
     *                       automatically call {@link
     *                       #notifyDataSetChanged}
     */
    @Override
    public void setNotifyOnChange(boolean notifyOnChange) {
        super.setNotifyOnChange(notifyOnChange);
    }

    private class ViewHolder{

        ImageButton senderIconButton;

        TextView senderNameText;

        TextView sendTimeText;

        TextView fromPlatformText;

        TextView contentText;

        GridView contentPictureGrid;

        ImageButton originalSenderIconButton;

        TextView originalSenderNameText;

        TextView originalContentText;

        GridView originalContentPicturesGrid;

        Button retransmitButton;

        Button commentButton;

        Button praiseButton;
    }
}
